/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author ACER
 */
public class TrayIconManager {
        // Única instância dessa classe.
        private static TrayIconManager trayIconManager;
        // A estrela desse artigo: o Tray Icon!
        private static TrayIcon trayIcon;
        // O ícone que aparecerá no System Tray.
        private static Image icon;
        // A "bandeja do sistema".
        private static SystemTray systemTray;
        /* Action Listener para o Tray Icon.
        ......* Um action event é disparado quando há um duplo clique no Tray Icon. */
        private static TrayIconActionListener trayIconActionListener;
        // O popup menu do tray icon.
        private static PopupMenu popup;
        // Itens do popup menu.
        private static final MenuItem menuTip = new MenuItem();
        private static final MenuItem menuSobre = new MenuItem();
        private static final MenuItem menuSair = new MenuItem();

        private TrayIconManager() {
            super();
        }

public static void setTrayIcon() {
    
    synchronized (TrayIconManager.class) {
        if (trayIconManager == null) {
            trayIconManager = new TrayIconManager();
        }
    }
    // Verifica se o sistema suporta system tray.
    if (SystemTray.isSupported()) {

    systemTray = SystemTray.getSystemTray();
    icon = Toolkit.getDefaultToolkit().getImage(
    TrayIconManager.class.getResource("/iconizinhot.png"));
    popup = new PopupMenu();
    
    // Inicializa tray icon.
    trayIcon = new TrayIcon(icon, "Dê dois cliques para maximizar/minimizar o programa", popup);
    // O ícone deve se enquadrar na bandeja do sistema.
    trayIcon.setImageAutoSize(true);
    // Adiciona um ActionListener ao tray icon.
    
    trayIconActionListener = new TrayIconActionListener() ;
    trayIcon.addActionListener(trayIconActionListener);
            
   // Configura menu popup.
    menuTip.setLabel("Dica do dia!");
    menuTip.addActionListener(new ShowTipOfTheDay());
    popup.add(menuTip);
    menuSobre.setLabel("Sobre ");
    menuSobre.addActionListener(new ShowAboutDialog());
    popup.add(menuSobre);
    popup.addSeparator();
    menuSair.setLabel("Sair");
    menuSair.addActionListener(new ExitApplication());
    popup.add(menuSair);
    // Adiciona o tray icon no system tray.
    try {
            systemTray.add(trayIcon);
    } catch (AWTException ex) {
            ex.printStackTrace();
    }
        
    trayIcon.displayMessage("Programa Iniciado...", "Zeit - Copyright© 2012, versão 1.0! ", TrayIcon.MessageType.INFO);
    } else {
    // Ops! A plataforma não provê, suporte para system trays.
    // Fazer algo aqui, como registrar num arquivo de log.
    }
    }

private static class TrayIconActionListener implements ActionListener {
    public void actionPerformed(final ActionEvent e) {
       
        trayIcon.displayMessage("Oh my!","You double-clicked the tray icon!", TrayIcon.MessageType.INFO);
    }
}

private static class ShowTipOfTheDay implements ActionListener {
    public void actionPerformed(final ActionEvent e) {
        trayIcon.displayMessage("Pensamento do dia!","A maior das habilidades é vencer os seus inimigos sem lutar!", TrayIcon.MessageType.INFO); 
      
    }
}

private static class ShowAboutDialog implements ActionListener {
    public void actionPerformed(final ActionEvent e) {
          JOptionPane.showMessageDialog(null, "Zeit - Copyright© 2012, versão 1.0!\n\nCreated by:\n"
                  + "Fanny Chien <fc2>\nGustavo Stor de Aguiar <gsa2>\nMariama Celi Serafim de Oliveira <mcso>\nPedro Henrique Rosendo Diniz <phrd>\nRafael Nunes Galdino da Silveira <rngs> "
                  + "\n\nDesigned by:\n Rafael Nunes and Fanny Chien"
                  + "\n\nTPZeit(Transport Procotocl Zeit) by: \nGustavo Stor and Pedro Diniz"
                  + "\n\nArchitetured by: \nMariama Celi and Luiz Neto" ,"Sobre", JOptionPane.INFORMATION_MESSAGE);
    }
}

private static class ExitApplication implements ActionListener {
    public void actionPerformed(final ActionEvent e) {
        System.exit(0);

    }
}
}